MAPS
Photo by Jeff Ackley on Unsplash
Water is life, and clean water means health…
— Audrey Hepburn
The voronoi map shows water points monitored over time by governments and development partners. The data is collected by the Water Point Data Exchange (WPDx) which aims to unlock the potential of water point data to improve decision-making and increase rural water access. Voronoi maps are based on the minimal distance needed to reach a landmark by using tessellation techniques that partition a plane into regions closest to these points. The darker the color of the region, the closer it is to the country’s capital city.
df <- read.csv("archetypes/water-access-points/water-access-points.csv", header = TRUE, stringsAsFactors = FALSE)
head(df, n=10)
country_capitals <- read.csv("archetypes/water-access-points/country-capitals.csv", header = TRUE, stringsAsFactors = FALSE)
country_capitals <- filter(country_capitals, ContinentName == "Africa")
head(country_capitals, n = 10)
df <- df %>%
filter(country_name %in% unique(country_capitals$CountryName)) %>%
mutate(
country_name = case_when(
country_name == "Congo - Kinshasa" ~ "Democratic Republic of the Congo",
country_name == "Congo - Brazzaville" ~ "Republic of Congo", # Republic of the Congo
#country_name == "Swaziland" ~ "Eswatini",
#country_name == "Tanzania" ~ "United Republic of Tanzania",
TRUE ~ country_name
))
df_wrangle <- merge(df, country_capitals, by.x = "country_name", by.y = "CountryName")
df_wrangle <- df_wrangle %>% select(row_id, country_name, lat_deg, lon_deg, water_source, water_tech, CapitalName, CapitalLatitude, CapitalLongitude, CountryCode)
df_wrangle <- df_wrangle %>%
mutate(
country_name = case_when(
#country_name == "Swaziland" ~ "Eswatini",
country_name == "Tanzania" ~ "United Republic of Tanzania",
TRUE ~ country_name
))
head(df_wrangle, n=10)
df_wrangle <-
df_wrangle %>%
mutate(lon_deg = if_else(lon_deg > 180, lon_deg - 360, lon_deg))
df_analytics <- df_wrangle %>%
rowwise() %>%
mutate(lat_deg = round(lat_deg, digits = 2), lon_deg = round(lon_deg, digits = 2)) %>%
mutate( dist_to_capital = distm ( x = c(lon_deg, lat_deg), y = c(CapitalLongitude, CapitalLatitude), fun = distHaversine))
df_analytics <- df_analytics %>% distinct(lon_deg, lat_deg, .keep_all = TRUE)
head(df_analytics, n=10)
theme_opts <- theme(
text = element_text(family = "inconsolata", size = 12),
plot.title = element_text(color = "black", size = 16, face = "bold", hjust = 0.5),
plot.subtitle = element_text(color = "black", size = 12, hjust = 0.5),
plot.caption = element_text(color = "#555555", size = 11),
plot.margin = margin(.25, .25, .25, .25, "cm"),
plot.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
legend.position = 'none'
)
draw_voronoi <- function(country) {
# map for specified country
map_sp <- ne_countries(scale = "small", country = country, returnclass = "sp")
# for scale limits
bounding_box <- bb(map_sp, projection=4326)
# filter data for specified country and limit coordinates to bounding box
df_country <- filter(df_analytics, country_name == country)
df_country <- filter(df_country, lon_deg >= bounding_box$xmin & lon_deg <= bounding_box$xmax)
df_country <- filter(df_country, lat_deg >= bounding_box$ymin & lat_deg <= bounding_box$ymax)
v <- df_country %>%
ggplot(aes(x = lon_deg, y = lat_deg)) +
# country borders
geom_polygon(data = map_sp, aes(long, lat), color = "grey20", fill = "#ffffff", size = 13.5) +
geom_polygon(data = map_sp, aes(long, lat), color = "white", fill = "#ffffff", size = 10) +
# alternative to country borders
# geom_voronoi(fill = "white", color = "grey25", size = 13.5, outline = map_sp) +
# geom_voronoi(fill = "white", color = "white", size = 10, outline = map_sp) +
# color by distance, outline using map
geom_voronoi(aes(fill = dist_to_capital, color = dist_to_capital), size = 0.3, outline = map_sp) +
# contour of water points
geom_density_2d(color = "white") +
# all water points
# geom_point(color = "white", alpha = 0.5, size = 0.5) +
# show capital city location
geom_point(data = head(df_country, n=1), aes(x = CapitalLongitude, y = CapitalLatitude), shape = 21, fill = "white", color = "black", size = 6) +
geom_point(data = head(df_country, n=1), aes(x = CapitalLongitude, y = CapitalLatitude), size = 3) +
scale_x_continuous(limits = c(bounding_box$xmin, bounding_box$xmax), expand = c(0.05, 0.05)) +
scale_y_continuous(limits = c(bounding_box$ymin, bounding_box$ymax), expand = c(0.05, 0.05)) +
scale_fill_gradient(low = "yellow", high = "red", na.value = NA) +
# lightened to see voronoi borders
scale_color_gradient(low = lighten("yellow", 0.25), high = lighten("red", 0.25), na.value = NA) +
coord_map(clip = "off") +
# labs(title = country, subtitle = "voronoi of water points, color by distance to capital city") +
theme_void() +
theme_opts
v
}
# list of countries to select from
# africa <- unique(df_analytics$country_name)
africa <- c(
"Algeria",
"Angola",
"Burkina Faso",
"Burundi",
"Central African Republic",
"Chad",
"Egypt",
"Ethiopia",
"Ghana",
"Kenya",
"Liberia",
"Madagascar",
"Mali",
"Mauritania",
"Mozambique",
"Namibia",
"Niger",
"Nigeria",
"Rwanda",
"Sierra Leone",
"South Africa",
"South Sudan",
"Sudan",
"Swaziland",
"United Republic of Tanzania",
"Uganda",
"Zambia",
"Zimbabwe"
)
uganda <- draw_voronoi("Uganda")
girafe(ggobj = uganda, width_svg = 16, height_svg = 16,
options = list(opts_sizing(rescale = TRUE, width = 0.5))
)